﻿using MesDAL.DBInfo;
using MesDAL.DTO;
using MesDAL.DTO.NetSetting;
using MesDAL.DTO.ProcessSheet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCMesServer.DBFunctions.TransformFuncs
{
    public class TransformDTOFunc
    {
        public static MachineInfoDTO TransformModel2MachineInfoDTO(machine_info machine)
        {
            MachineInfoDTO dto = new MachineInfoDTO()
            {
                id = machine.id,
                machine_state = machine.machine_state,
                machine_supplier = machine.machine_supplier,
                machine_num = machine.machine_num,
                machine_class = machine.machine_class,
                machine_name = machine.machine_name
            };
            return dto;
        }

        internal static machine_info TransformMachineInfo2Model(MachineInfoDTO machineInfo)
        {
            machine_info info = new machine_info()
            {
                id = machineInfo.id,
                machine_state = machineInfo.machine_state,
                machine_num = machineInfo.machine_num,
                machine_class = machineInfo.machine_class,
                machine_name = machineInfo.machine_name,
                machine_supplier = machineInfo.machine_supplier,
            };
            return info;
        }

        internal static machinestate_log TransformMachineState2Model(MachineStateLogDTO dto)
        {
            machinestate_log state = new machinestate_log()
            {
                id = dto.id,
                after_state = dto.after_state,
                before_state = dto.before_state,
                event_time = dto.event_time,
                machine_num = dto.machine_num
            };
            return state;
        }

        internal static MachineStateLogDTO TransformModel2MachineStateDTO(machinestate_log statelog)
        {
            MachineStateLogDTO dto = new MachineStateLogDTO()
            {
                id = statelog.id,
                after_state = statelog.after_state,
                event_time = statelog.event_time,
                before_state = statelog.before_state,
                machine_num = statelog.machine_num
            };
            return dto;
        }

        public static EquipNetSettingDTO TransformModel2EquipNetSettingDTO(equipnetsetting_info info)
        {
            EquipNetSettingDTO dto = new EquipNetSettingDTO()
            {
                EquipmentNum = info.equip_num,
                IpConfig = info.ip_config,
            };
            return dto;
        }

        public static workpiece_info TransformWorkPiece2Model(WorkpieceDTO dto)
        {
            workpiece_info info = new workpiece_info()
            {
                id = dto.id,
                size = dto.size,
                date = dto.date,
                deadline = dto.deadline,
                name = dto.name,
                number = dto.number,
                order = dto.order,
                qty = dto.qty,
                material = dto.material,
            };
            return info;
        }

        public static WorkpieceDTO TransformModel2WorkPieceDTO(workpiece_info info)
        {
            WorkpieceDTO dto = new WorkpieceDTO()
            {
                id = info.id,
                size = info.size,
                date = info.date,
                deadline = info.deadline,
                order = info.order,
                name = info.name,
                number = info.number,
                qty = info.qty,
                material = info.material,
            };
            return dto;
        }

        internal static processsheet_log TransformProcessSheet2Model(ProcessSheetDTO dto)
        {
            processsheet_log log = new processsheet_log()
            {
                id = dto.id,
                deadline = dto.deadline,
                content = dto.content,
                estimatedhours = dto.estimatedhours,
                name = dto.name,
                number = dto.number,
                //workpiece_number = dto.workpiece.number,
                workpiece_id = dto.workpiece.id,
            };
            return log;
        }

        internal static ProcessSheetDTO TransformModel2ProcessSheetDTO(processsheet_log addlog)
        {
            ProcessSheetDTO dto = new ProcessSheetDTO()
            {
                id = addlog.id,
                deadline = addlog.deadline,
                content = addlog.content,
                estimatedhours = addlog.estimatedhours,
                name = addlog.name,
                number = addlog.number,
                workpiece = new WorkpieceDTO()
                {
                    id = addlog.workpiece_id
                }
            };
            return dto;
        }

        public static ProduceplanDTO TransformModel2ProduceplanDTO(produceplan_log log)
        {
            ProduceplanDTO planDTO = new ProduceplanDTO()
            {
                id = log.id,
                estimated_start = log.estimated_start,
                estimated_end = log.estimated_end,
                machineInfo = new MachineInfoDTO() { machine_num = log.machine_num },
                processSheet = new ProcessSheetDTO() { id = log.process_id },
            };
            if (log.discontinuous_date is null)
            {
                planDTO.discontinuous_date = false;
            }
            else
            {
                planDTO.discontinuous_date = bool.Parse(log.discontinuous_date);
            }
            return planDTO;
        }

        public static produceplan_log TransfromProducePlan2Model(ProduceplanDTO dto)
        {
            produceplan_log log = new produceplan_log()
            {
                id = dto.id,
                estimated_end = dto.estimated_end,
                estimated_start = dto.estimated_start,
                machine_num = dto.machineInfo.machine_num,
                priority = dto.priority,
                process_id = dto.processSheet.id,
                discontinuous_date = dto.discontinuous_date.ToString(),
            };
            return log;
        }
    }
}
